<template>
  <div>
    <div style="height: 40px;background: #E6F7FF;line-height: 40px;padding-left:10px">{{ labourInfo?(labourInfo.laborDispatch?labourInfo.laborDispatch+'人':''):'' }}</div>
    <BaseTableCheck
      ref="labourtable"
      :loading="loading"
      :data="tableData"
      :columns="columns" 
      :tableHT="tableHt" 
      :pagination="false" 
      @checkId="selectCheckHandelerClick">
    </BaseTableCheck>
      <div class="demo-drawer__footer">
        <el-button
          size="small"
          type="primary"
          @click="addUpdate()"
        >保 存</el-button>
      </div>
  </div>
</template>
<script>
import BaseTableCheck from '@/components/BaseTable/baseTablecheck'
import api from '@/api/Product/secondryDispenser/index'
export default {
  components: { BaseTableCheck },
  props: {
    labourInfo: {
      type: Object,
    },
    colseDrawer: {
      type: Function
    }
  },
  data() {
    return {
      loading: false,
      tableHt: window.innerHeight - 160,
      tableData: [],
      columns: [
        {
          prop: 'userNo',
          label: '工号'
        },
        {
          prop: 'operatorsName',
          label: '姓名'
        },
        {
          prop: 'deptName',
          label: '队别'
        },
      ],
      selectCheck: [], // 选中的复选框
      dispatchList: [], // 已派工劳务
    }
  },
  mounted() {
    this.queryLabour()
    this.querydispatchLabour()
  },
  methods: {
    // 查询劳务
    queryLabour () {
      this.$refs.labourtable.clearSelection()
      api.queryLaborByDeptId(this.labourInfo).then(res => {
        this.tableData = res.data.data
      })
        
    },
    // 选中复选框
    selectCheckHandelerClick (val) {
      this.selectCheck = val
    },
    // 新增修改
    addUpdate () {
      if (this.selectCheck.length == 0) {
        return this.$message.warning('请选中需要派工的人员')
      }
      this.selectCheck.map(v => {
        v.operatorsId = v.operatorsId + ''
        v.dispatchType = '2'
      })
      let params = {
        planId: this.labourInfo.id + '',
        list: this.selectCheck
      }
      api.insertLabor(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.$message.success('保存成功')
        this.colseDrawer()
      })
    },
    // 查询已派工劳务
    querydispatchLabour () {
      let params = {
        planId: this.labourInfo.id,
        dispatchType: '2'
      }
      api.queryById(params).then(res => {
        this.dispatchList = res.data.data

        if (this.dispatchList && this.dispatchList.length > 0) {
          this.dispatchList.forEach(v => {
            this.tableData.forEach(i => {
              if (v.operatorsId == i.operatorsId) {
                i.isCheck = '1'
              }
            })
          })
          let checkedGuide = this.tableData.filter(function (s) {
						return s.isCheck == "1"
					})
          //默认选中
					this.$nextTick(() => {
						checkedGuide.forEach(row => {
							this.$refs.labourtable.toggleRowSelection(row, true)
						})
					})
        }
      })
    },
  }
}
</script>
<style lang="less" scoped>

</style>